package com.tongfusoft.document.web.controller.persionCenter;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfuosft.document.system.notice.service.INoticeService;
import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.Notice;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.personalCenter.myFollow.service.IMyFollowService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;

@RequestMapping("/personalCenter")
@Controller
public class MyNoticecontroller extends BaseController {

	@Autowired
	@Qualifier("noticeService")
	public INoticeService noticeService;
	@Autowired
	@Qualifier("userFollowService")
	public IMyFollowService userFollowService;
	
	@UrlDesc(value = "关注文档更新通知页")
	@RequestMapping("/myNotice")
	public String myCollection(Model model){
		
		model.addAttribute("mainPageKey", "myNotice");
		model.addAttribute("mainPage", "personalCenter/myNotice");
		return "base";
	}
	
	@UrlDesc(value = "当前用户关注文档更新列表")
	@RequestMapping("/noticeDocs/query")
	@ResponseBody
	public PageInfo noticeDocsquery(Model model,String userId,Notice notice,int pageIndex, int pageSize,String query){
		PageInfo pageInfo =  new PageInfo();
		try {
			notice.setUserId(userId);
			if(StringUtils.isNotEmpty(query)){
				Map queryMap = JsonUtils.getMap4Json(query);
				if(queryMap.get("fastText")!=null){
					notice.setDocTitle(queryMap.get("fastText").toString());
				}
			}
			pageInfo = this.noticeService.notice((Users)super.getUser(), notice, pageSize, pageIndex);
			
		} catch (Exception e) {

			e.printStackTrace();
		}
		return pageInfo;

	}
	
	@UrlDesc(value = "删除关注文档更新")
	@RequestMapping("/notice/del")
	@ResponseBody
	public ResultModel delNotice(Notice notice){
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			this.noticeService.delNotice(notice,(Users)super.getUser());
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;
	}
	
	@UrlDesc(value = "当前用户关注文档更新记录")
	@RequestMapping("/notice/getNumber")
	@ResponseBody
	public ResultModel getNoticeNumber(String userId){
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			rm.setData(this.noticeService.noticeByUserId(userId)); 
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;
	}
	
	@UrlDesc(value = "当前用户关注文档更新记录数(分页)")
	@RequestMapping("/notice/getNumberPage")
	@ResponseBody
	public PageInfo getNoticePageNumber(Model model,String userId,int pageIndex, int pageSize){
		PageInfo pageInfo =  new PageInfo();
		try {
			pageInfo = this.noticeService.noticePageByUserId(userId, pageSize, pageIndex);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageInfo;
	}
}
